/**
 * 
 */
//输入不能为空
function InputNotNull(value,str){
	if(value.val().length == 0){
		ErrorTipBottomCenter(str);
        return false;
	}else{ 
		return true;
	}
}
//检验添加输入是否有误
function CreatVerify(){
	if(Cheaktitle($('#title')) && Cheakname($('#name')) && Cheakauthor($('#author')) && CheakReadings($('#readings')) && CheakType($('#type')) ){
		return true;
	}
	return false;
}
//检验更新输入是否有误
function UpdateVerify(){
	if(Cheaktitle($('#edit_title')) && Cheakname($('#edit_name')) && Cheakauthor($('#edit_author')) && CheakReadings($('#edit_readings')) && CheakType($('#edit_type')) ){
		return true;
	}
	return false;
}
//验证标题
function Cheaktitle(value){
	return InputNotNull(value,"标题不能为空！");
}
//验证书名
function Cheakname(value){
	return InputNotNull(value,"书名不能为空！");
}
//验证作者
function Cheakauthor(value){
	return InputNotNull(value,"作者不能为空！");
}
//验证阅读量
function CheakReadings(value){
	var readings=InputNotNull(value,"阅读量不能为空！");
	if(readings){
		if(isNaN(value.val())){
			ErrorTipBottomCenter('阅读量请输入数字');
			return false;
		}else{
			if(value.val()>2147483647){
				ErrorTipBottomCenter('阅读量数字输入有误，请重新输入小于2147483647');
				return false;
			}
			return true;
		}
	}
	return false;
}
//检验状态
function CheakType(value){
	var type=value.val();
	if(type==0){
		ErrorTipBottomCenter('请选择状态');
		return false
	}else{
		return true;
	}
}
//添加数据
function Create(){
	if(CreatVerify()){
		var json = {
			"title":$('#title').val()==null?"":$('#title').val(),
			"name":$('#name').val()==null?"":$('#name').val(),
			"author":$('#author').val()==null?"":$('#author').val(),
			"readings":$('#readings').val()==null?"":$('#readings').val(),
			"type":$('#type').val()===0?"0":$('#type').val(),
		};
		var postdata = JSON.stringify(json);//json对象转换json字符串
		$.ajax({
			type:"post",
			contentType: 'application/json',
			url:"book/add.do",
			datatype: "json", 
			async:true,
			data:postdata,
			success:function(result) {
				if(result.isOK==true){
					$('#creater').modal('hide');//隐藏新增信息模态框
					//清空模态框中的信息
					$('#title').val('');
					$('#name').val('');
					$('#author').val('');
					$('#readings').val('');
					$('#type').val('');
					SuccessTipBottomRight(result.successMes);
					SelectPage(1);
				}else{
					ErrorTipBottomCenter(result.errorMes);
				}
			},
			error:function(){
				AjaxError();
			}
		});
	}
}
//删除数据
function Delete(ID,PageNo){
	swal({
        title: "确认删除？",
        text: "您将无法恢复这个删除操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "删除！",
        cancelButtonText: "取消",
        closeOnConfirm: true
    }, function(isConfirm) {
        if (isConfirm) {
        	if(ID>0){
        		$.ajax({
        			type:"post",
        			url:"book/delete.do",
        			datatype:"json",
        			async:"true",
        			data:{
        				"book_id":ID
        			},
        			success:function (result){
        				//信息删除成功
        				if(result.isOK==true){
        					SuccessTipBottomRight(result.successMes);
        					//重载加载页面内容
        					SelectPage(PageNo);
        				}else{
        					ErrorTipBottomCenter(result.errorMes);
        					SelectPage(PageNo);
        				}
        			},
        			error:function(){
        				AjaxError();
        			}
        		})
        	}      
        }
    	})
}
//信息编辑
function Edit(ID,PageNo){
	if(ID>0){
		$.ajax({
			type:"post",
			url:"book/info.do",
			datatype:"json",
			async:"true",
			data:{
				"book_id":ID,
			},
			success:function (result){
				var book = result.bookdate;
				//显示模态框
				$('#edit_book').modal('show');
				//保存页码
				$('#edit_pageNo').val(PageNo);
				//显示信息
				$('#edit_id').val(book.id);
				$('#edit_title').val(book.title);
				$('#edit_name').val(book.name);
				$('#edit_author').val(book.author);
				$('#edit_readings').val(book.readings);
				$('#edit_type').val(book.type);
			},
			error:function(){
				AjaxError();
			}
		})
	}else{
		ErrorTipBottomLeft('操作失败,请联系相关技术人员解决问题！');
	}
}
//更新信息
function Update(){
	if(UpdateVerify()){
		var json = {
			"id":$('#edit_id').val()==null?"":$('#edit_id').val(),
			"title":$('#edit_title').val()==null?"":$('#edit_title').val(),
			"name":$('#edit_name').val()==null?"":$('#edit_name').val(),
			"author":$('#edit_author').val()==null?"":$('#edit_author').val(),
			"readings":$('#edit_readings').val()==null?"":$('#edit_readings').val(),
			"type":$('#edit_type').val()==0?"0":$('#edit_type').val(),
		};
		var postdata = JSON.stringify(json);//json对象转换json字符串
		$.ajax({
			type:"post",
			url:"book/update.do",
			datatype: "json",
			contentType: 'application/json',
			async:true,
			data:postdata,
			success:function(result) {
				if(result.isOK==true){
					$('#edit_book').modal('hide');//隐藏新增信息模态框
					//清空模态框中的信息
					$('#edit_id').val('');
					$('#title').val('');
					$('#name').val('');
					$('#author').val('');
					$('#readings').val('');
					$('#type').val('');
					SuccessTipBottomRight(result.successMes);
					SelectPage($('#edit_pageNo').val());
					$('#edit_pageNo').val('');
				}else{
					ErrorTipBottomCenter(result.errorMes);
				}
			},
			error:function(){
				AjaxError();
			}
		});
	}
}
//检验是否有选中
function CheakBox(){
	var IDlist = "";
    $('input:checkbox[name=baseTable]:checked').each(function(i){
    	if(0==i){
    		IDlist = $(this).val();
    	}else{
    		IDlist += (","+$(this).val());
    	}
    });
    return IDlist;
}
//取消选中
function CancelCheakBox(){
	//取消选中
	$('input:checkbox[name=baseTable]:checked').each(function(i){
		$(this).attr("checked",false);
	});
	$('#check-all').attr("checked",false);
}
//批量启用
function BatchEnable(IDlist){
	$.ajax({
		type:"post",
		url:"book/batchEnable.do",
		datatype: "json", 
		async:true,
		data:{
			"IDlist":IDlist,
		},
		success:function(result) {
			if(result.isOK==true){
				SuccessTipBottomRight(result.successMes);
				//取消选中
				CancelCheakBox();
				//重新加载数据
		    	SelectPage(1);
			}else{
				ErrorTipBottomCenter(result.errorMes);
				//重新加载数据
		    	SelectPage(1);
			}
		},
		error:function(){
			AjaxError();
		}
	});
}
//批量禁用
function BatchDisable(IDlist){
	$.ajax({
		type:"post",
		url:"book/batchDisable.do",
		datatype: "json", 
		async:true,
		data:{
			"IDlist":IDlist,
		},
		success:function(result) {
			if(result.isOK==true){
				SuccessTipBottomRight(result.successMes);
				//取消选中
				CancelCheakBox();
				//重新加载数据
		    	SelectPage(1);
			}else{
				ErrorTipBottomCenter(result.errorMes);
				//重新加载数据
		    	SelectPage(1);
			}
		},
		error:function(){
			AjaxError();
		}
	});
}
//批量删除 
function BatchDelete(IDlist){
	$.ajax({
		type:"post",
		url:"book/batchDelete.do",
		datatype: "json", 
		async:true,
		data:{
			"IDlist":IDlist,
		},
		success:function(result) {
			if(result.isOK==true){
				SuccessTipBottomRight(result.successMes);
				//取消选中
				CancelCheakBox();
				//重新加载数据
		    	SelectPage(1);
			}else{
				ErrorTipBottomCenter(result.errorMes);
				SelectPage(1);
			}
		},
		error:function(){
			AjaxError();
		}
	});
}
//页面加载完毕
$(function (){
	//添加信息
	$('#addBtn').click(function (){
		Create();
	})
	//更新信息
	$('#updateBtn').click(function (){
		Update();
	})
	//批量启用
	$('#startBtn').click(function (){
		var IDlist = CheakBox();
	    if(IDlist.length==0){
	    	ErrorTipBottomCenter('请选择书籍编号');
	    	return false;
	    }else{
	    	swal({
		        title: "你确定？",
		        text: "确定要启用这些书籍吗？",
		        type: "info",
		        showCancelButton: true,
		        confirmButtonColor: "#DD6B55",
		        confirmButtonText: "启用！",
		        cancelButtonText: "取消",
		        closeOnConfirm: true,
		        closeOnCancel: false,
		    }, function(isConfirm) {
		        if (isConfirm) {
		        	BatchEnable(IDlist);
		        } else{
		        	swal({
				        title: "取消!",
				        text: "书籍未启用",
				        type:"success",
				        timer: "1000",
				        showConfirmButton: false
	    			})
	    			CancelCheakBox();
		        }
		    })
	    }
	})
	//批量禁用
	$('#disableBtn').click(function (){
		var IDlist = CheakBox();
	    if(IDlist.length==0){
	    	ErrorTipBottomCenter('请选择书籍编号');
	    	return false;
	    }else{
	    	swal({
		        title: "你确定？",
		        text: "确定禁用这些书籍吗？",
		        type: "warning",
		        showCancelButton: true,
		        confirmButtonColor: "#DD6B55",
		        confirmButtonText: "禁用！",
		        cancelButtonText: "取消",
		        closeOnConfirm: true,
		        closeOnCancel: false
		    }, function(isConfirm) {
		        if (isConfirm) {
		        	BatchDisable(IDlist);

		        } else{
		        	swal({
				        title: "取消!",
				        text: "书籍未禁用",
				        type:"success",
				        timer: "1000",
				        showConfirmButton: false
	    			})
	    			CancelCheakBox();
		        }
		    })
	    }
	})
	//批量删除
	$('#deleteBtn').click(function (){
		var IDlist = CheakBox();
	    if(IDlist.length==0){
	    	ErrorTipBottomCenter('请选择书籍编号');
	    	return false;
	    }else{
	    	swal({
		        title: "你确定？",
		        text: "删除操作无法恢复！",
		        type: "error",
		        showCancelButton: true,
		        confirmButtonColor: "#DD6B55",
		        confirmButtonText: "删除！",
		        cancelButtonText: "取消",
		        closeOnConfirm: true,
		        closeOnCancel: false
		    }, function(isConfirm) {
		        if (isConfirm) {
		        	BatchDelete(IDlist);
		        } else{
		        	swal({
				        title: "取消!",
				        text: "信息未删除",
				        type:"error",
				        timer: "1000",
				        showConfirmButton: false
	    			})
	    			CancelCheakBox();
		        }
		    })
	    }
	})
})